﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.IO;

namespace BaseData.Common
{
    /// <summary>
    /// 邮件处理类
    /// </summary>
    public class Email
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="subject">主题</param>
        /// <param name="body">邮件正文</param>
        /// <param name="mailTo">收件人</param>
        /// <returns>发送结果</returns>
        public static bool Send(string subject, string body, string mailTo)
        {
            if (string.IsNullOrEmpty(mailTo))
            {
                return false;
            }
            try
            {
                using (MailMessage mm = new MailMessage())
                {
                    mm.IsBodyHtml = true;
                    mm.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");
                    mm.Subject = subject;
                    mm.Body = body;
                    //mm.To.Add(new MailAddress(mailTo));
                    mailTo = mailTo.Replace(';', ',');
                    mm.To.Add(mailTo);
                    SmtpClient mailClient = new SmtpClient();
                    mailClient.Send(mm);
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 发送邮件
        /// </summary>
        public static bool Send(string subject, string body)
        {
            bool result = true;
            string mailTo = ConfigurationManager.AppSettings["MailTo"];
            if (!string.IsNullOrEmpty(mailTo))
            {
                result = Send(subject, body, mailTo);
            }
            return result;
        }
    }
}
